from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from jnius import autoclass

# 获取 Android 的 Java 类
PythonActivity = autoclass('org.kivy.android.PythonActivity')
Context = autoclass('android.content.Context')
SensorManager = autoclass('android.hardware.SensorManager')
AudioManager = autoclass('android.media.AudioManager')

class HardwareTestApp(App):
    def build(self):
        self.layout = BoxLayout(orientation='vertical')
        
        # 检测麦克风
        self.mic_label = Label(text="麦克风: 未知")
        self.layout.add_widget(self.mic_label)
        self.check_microphone()
        
        # 检测震动
        self.vibrator_label = Label(text="震动: 未知")
        self.layout.add_widget(self.vibrator_label)
        self.check_vibrator()
        
        # 检测传感器
        self.sensor_label = Label(text="传感器: 未知")
        self.layout.add_widget(self.sensor_label)
        self.check_sensors()
        
        return self.layout

    def check_microphone(self):
        # 获取 AudioManager 实例
        audio_manager = PythonActivity.mActivity.getSystemService(Context.AUDIO_SERVICE)
        if audio_manager.isMicrophoneMute():
            self.mic_label.text = "麦克风: 已静音"
        else:
            self.mic_label.text = "麦克风: 可用"

    def check_vibrator(self):
        # 获取 Vibrator 实例
        vibrator = PythonActivity.mActivity.getSystemService(Context.VIBRATOR_SERVICE)
        if vibrator.hasVibrator():
            self.vibrator_label.text = "震动: 可用"
        else:
            self.vibrator_label.text = "震动: 不可用"

    def check_sensors(self):
        # 获取 SensorManager 实例
        sensor_manager = PythonActivity.mActivity.getSystemService(Context.SENSOR_SERVICE)
        sensors = sensor_manager.getSensorList(SensorManager.SENSOR_TYPE_ALL)
        if sensors:
            self.sensor_label.text = f"传感器: 可用 ({len(sensors)} 个)"
        else:
            self.sensor_label.text = "传感器: 不可用"

if __name__ == '__main__':
    HardwareTestApp().run()
